///////////////////////////////////////////////////////////////////////////////
//
// Licensed under the MIT License
//
// Copyright (c) 2010 Chris Colinsky
// All Rights Reserved
//
// version	0.5
// http://code.google.com/p/sugarcookie/
// http://code.google.com/p/sugarcookie/wiki/License
//
////////////////////////////////////////////////////////////////////////////////

package sugarcookie.validators {
	/**
	 * Validates phone numbers.
	 */
	public class PhoneNumberValidaor {
		private static const ALLOWED_FORMAT_CHARS:RegExp = /[()-.+]/;
		private static const  PHONE_REGEX:RegExp = /^\(\d{3}\)[ -]\d{3}[ -]\d{4}$/;
		
		//   Valid Formats
		//   only validates US phone numbers
		//   123 123 4567
		//   123-123-4567
		//   (123) 123 4567
		//   (123) 123-4567
		//   123.123.4567
		
		
		public static function validate(str:String):ValidationResult {
			if (!ALLOWED_FORMAT_CHARS.test(str)) return new ValidationResult("Phone number contains restricted characters",true);
			else if (!PHONE_REGEX.test(str)) return new ValidationResult("Phone number is not a valid format",true);
			else return new ValidationResult("Phone number is valid",false);
		}
	}
}